using System;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for MapAdaptor.
	/// </summary>
	public class MapAdaptor : Adaptor, IRefreshable, IDeletable
	{
    private Map map;
		public MapAdaptor(Map map) : base()
		{
		  this.map = map;	
    }

    public override void AddChildren(System.Windows.Forms.TreeNode node)
    {
      if (map.IsAdministration)
      {
        // admin forms
        TreeNode formsNode = TreeNodeFactory.CreateTreeNode(new AdminFormsAdaptor(map.CreationActions));
        node.Nodes.Add(formsNode);
      }
      else
      {
        // creation actions
        TreeNode actionsNode = TreeNodeFactory.CreateTreeNode(new ActionsAdaptor(map.CreationActions));
        node.Nodes.Add(actionsNode);
        // stages
        TreeNode stagesNode = TreeNodeFactory.CreateTreeNode(new StagesAdaptor(map.Stages));
        node.Nodes.Add(stagesNode);
        // folders
        if (map.Folders.Count > 0)
        {
          TreeNode folderNode = TreeNodeFactory.CreateTreeNode(new FoldersAdaptor(map.Folders));
          node.Nodes.Add(folderNode);
        }

        // forms
        if (map.Forms.Count > 0)
        {
          TreeNode formsNode = TreeNodeFactory.CreateTreeNode(new FormsAdaptor(map.Forms));
          node.Nodes.Add(formsNode);
        }
      }

      // variables
      if (map.Variables.Count > 0)
      {
        TreeNode variablesNode = TreeNodeFactory.CreateTreeNode(new VariablesAdaptor(map.Variables));
        node.Nodes.Add(variablesNode);
      }

      // scripts
      if (map.Scripts.Count > 0)
      {
        TreeNode scriptsNode = TreeNodeFactory.CreateTreeNode(new ScriptsAdaptor(map.Scripts));
        node.Nodes.Add(scriptsNode);
      }

      // designer log
      if (map.DesignerLog.Count > 0)
      {
        TreeNode designerLogNode = TreeNodeFactory.CreateTreeNode(new DesignerLogAdaptor(map.DesignerLog));
        node.Nodes.Add(designerLogNode);
      }
    }

    public override int ImageIndex
    {
      get
      {
        if (map.IsAdministration)
          return 3;
        else
          return 2;
      }
    }

    public override string Text
    {
      get
      {
        return map.Name;
      }
    }

    public override object Object
    {
      get
      {
        return map;
      }
    }

    public override Control GetRenderer()
    {
      MapControl mapControl = new MapControl();
      mapControl.LoadMap(map);
      mapControl.ItemDeleted += new EventHandler(TriggerItemDeleted);
      mapControl.RefreshRequired += new EventHandler(TriggerRefreshRequired);
      return mapControl;
    }

    #region IRefreshable Members

    public void Refresh()
    {
      map.Refresh();
    }

    #endregion
    #region IDeletable Members

    public void Delete()
    {
      map.Delete();
    }

    #endregion
  }
}
